import { checkConnectSetting, type connectSetting } from "@/ts/connect/connection";
import type connection from "@/ts/connect/connection";
import { defineStore } from "pinia";
import { ref } from "vue";

export const useServers = defineStore("servers", () => {
    const roomServer = ref('')
    const roomConnection = ref(undefined as connection | undefined)
    const connectSetting = ref({ type: 'websocket' } as connectSetting)
    return { roomServer, roomConnection, connectSetting}
}, {
    persist: {
        pick: ['roomServer', 'connectSetting'],
        serializer:{
            serialize(data) {
                return JSON.stringify(data)
            },
            deserialize(data) {
                let state = JSON.parse(data)
                if(typeof state.roomServer!='string')state.roomServer = state.roomServer.toString()
                state.connectSetting = checkConnectSetting(state.connectSetting)
                return state
            },
        }
    }
})